﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using GH.BLL;


    public partial class VipMemberSearch : GH.Web.BasePage.PageBase
    {
        public static string strWhere;
        #region 画面初始化

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //操作其他单位会员权限
                if (!this.GetRight(201))
                {
                    strWhere="  单位=" + int.Parse(Session["Union"].ToString());
                }
                else
                {
                    strWhere = " 1=1 ";
                }
                
                DataBindDdlUnit();
                DataBindDdlDepartment();
                DataBindDdlGroup();
                DataBindAttr();
                DataBindEducation();
                DataBindPolitical();
                DataBindTitle();
                SetDataInit(1);
            }

        }
        void DataBindDdlUnit()
        {
            GH.BLL.Union unitBLL = new Union();
            System.Text.StringBuilder strWhere = new System.Text.StringBuilder();
            //操作其他单位会员权限
            if (!this.GetRight(201))
            {
                strWhere.AppendFormat(" 工会单位信息.ID={0}", int.Parse(Session["Union"].ToString()));
            }
            else
            {
                strWhere.AppendFormat("");
            }
            DataTable dtQuery = unitBLL.GetList(strWhere.ToString()).Tables[0];
            this.ddlUnit.DataSource = dtQuery;
            this.ddlUnit.DataTextField = "单位名称";
            this.ddlUnit.DataValueField = "ID";
            this.ddlUnit.DataBind();
            //操作其他单位会员权限
            if (this.GetRight(201))
            {
                ddlUnit.Items.Insert(0, new ListItem("请选择", "-1"));
                ddlDepartment.SelectedValue = "-1";
            }
            
        }

        void DataBindDdlDepartment()
        {
            int unitID = Convert.ToInt32(this.ddlUnit.SelectedValue);
            if (unitID != -1)
            {
                this.ddlDepartment.Items.Clear();
                GH.BLL.DepartMent DepartMentBLL = new DepartMent();
                this.ddlDepartment.DataSource = DepartMentBLL.GetModelList("单位ID=" + unitID);
                this.ddlDepartment.DataTextField = "部门名称";
                this.ddlDepartment.DataValueField = "ID";
                this.ddlDepartment.DataBind();
                ddlDepartment.Items.Insert(0, new ListItem("请选择", "-1"));

                ddlDepartment.SelectedValue = "-1";
            }
            else
            {
                this.ddlDepartment.Items.Clear();

                ddlDepartment.Items.Insert(0, new ListItem("请选择", "-1"));

                ddlDepartment.SelectedValue = "-1";
            }

        }


        void DataBindDdlGroup()
        {
            int unitID = Convert.ToInt32(this.ddlUnit.SelectedValue);
            if (unitID != -1)
            {
                this.ddlGroup.Items.Clear();
                GH.BLL.Group GroupBLL = new Group();
                this.ddlGroup.DataSource = GroupBLL.GetModelList("单位ID=" + unitID);
                this.ddlGroup.DataTextField = "班组名称";
                this.ddlGroup.DataValueField = "ID";
                this.ddlGroup.DataBind();
                ddlGroup.Items.Insert(0, new ListItem("请选择", "-1"));

                ddlGroup.SelectedValue = "-1";
            }
            else
            {
                this.ddlGroup.Items.Clear();

                ddlGroup.Items.Insert(0, new ListItem("请选择", "-1"));

                ddlGroup.SelectedValue = "-1";
            }

        }

        protected void ddlUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataBindDdlDepartment();
            DataBindDdlGroup();
        }

        private void DataBindAttr()
        {
            this.ddlAttr.Items.Clear();
            Common.SetDataBound(this.ddlAttr, "104");
            ddlAttr.Items.Insert(0, new ListItem("请选择", "-1"));

            ddlAttr.SelectedValue = "-1";

        }

        private void DataBindEducation()
        {
            this.ddlEducation.Items.Clear();
            Common.SetDataBound(this.ddlEducation, "101");
            ddlEducation.Items.Insert(0, new ListItem("请选择", "-1"));

            ddlEducation.SelectedValue = "-1";

        }

        private void DataBindPolitical()
        {
            this.ddlPolitical.Items.Clear();
            Common.SetDataBound(this.ddlPolitical, "102");
            ddlPolitical.Items.Insert(0, new ListItem("请选择", "-1"));

            ddlAttr.SelectedValue = "-1";

        }
        private void DataBindTitle()
        {
            this.ddlTitle.Items.Clear();
            Common.SetDataBound(this.ddlTitle, "103");
            ddlTitle.Items.Insert(0, new ListItem("请选择", "-1"));

            ddlTitle.SelectedValue = "-1";
        }

        public string GetUnitName(object o)
        {
            try
            {


                GH.BLL.Union unitBLL = new Union();
                return unitBLL.GetModel(Convert.ToInt32(o)).单位名称;
            }
            catch (Exception)
            {

                return "";
            }
        }

        public string GetDepartmentName(object o)
        {
            try
            {


                if (Convert.ToInt32(o) == -1)
                {
                    return "";
                }
                else
                {
                    GH.BLL.DepartMent DepartMentBLL = new DepartMent();
                    return DepartMentBLL.GetModel(Convert.ToInt32(o)).部门名称;
                }
            }
            catch (Exception)
            {

                return "";
            }
        }

        public string GetGroupName(object o)
        {
            try
            {
                if (Convert.ToInt32(o) == -1)
                {
                    return "";
                }
                else
                {



                    GH.BLL.Group GroupBLL = new Group();
                    return GroupBLL.GetModel(Convert.ToInt32(o)).班组名称;
                }
            }
            catch
            {
                return "";
            }
        }

        public string GetAttrName(object o)
        {
            GH.BLL.User UserLL = new User();
            return UserLL.GetModel("104", o.ToString()).Tables[0].Rows[0]["区分名称"].ToString();
        }
        public string GetEducationName(object o)
        {
            GH.BLL.User UserLL = new User();
            return UserLL.GetModel("101", o.ToString()).Tables[0].Rows[0]["区分名称"].ToString();
        }
        public string GetPoliticalName(object o)
        {
            GH.BLL.User UserLL = new User();
            return UserLL.GetModel("102", o.ToString()).Tables[0].Rows[0]["区分名称"].ToString();
        }
        public string GetTitleName(object o)
        {
            GH.BLL.User UserLL = new User();
            return UserLL.GetModel("103", o.ToString()).Tables[0].Rows[0]["区分名称"].ToString();
        }


        #endregion

        #region 画面方法

        private void SetDataInit(int PageIndex)
        {
            VipMember bllVip = new VipMember();
            DataTable dtQuery = bllVip.GetPageList(this.AspNetPager1.PageSize, PageIndex, strWhere).Tables[0];
            this.AspNetPager1.RecordCount = bllVip.GetCount(strWhere);
            rptList.DataSource = dtQuery;
            rptList.DataBind();

        }

        #endregion


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            SetDataInit(this.AspNetPager1.CurrentPageIndex);
        }

        public void CreateExcel(DataSet ds, string typeid, string FileName)
        {
            HttpResponse resp;
            resp = Page.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            string colHeaders = "", ls_item = "";
            int i = 0;

            //定义表对象与行对像，同时用DataSet对其值进行初始化
            DataTable dt = ds.Tables[0];
            DataRow[] myRow = dt.Select("");
            // typeid=="1"时导出为EXCEL格式文件；typeid=="2"时导出为XML格式文件
            if (typeid == "1")   //取得数据表各列标题，各标题之间以\t分割，最后一个列标题后加回车符
            {
                for (i = 0; i < dt.Columns.Count - 1; i++)
                    colHeaders += " " + dt.Columns[i].Caption.ToString() + "\t";
                colHeaders += " " + dt.Columns[i].Caption.ToString() + "\n";    //向HTTP输出流中写入取得的数据信息
                resp.Write(colHeaders);
                foreach (DataRow row in myRow) //逐行处理数据 
                {
                    //在当前行中，逐列获得数据，数据之间以\t分割，结束时加回车符\n
                    for (i = 0; i < dt.Columns.Count - 1; i++)
                        ls_item += "" + row[i].ToString() + "\t";
                    ls_item += "" + row[i].ToString() + "\n";
                    //当前行数据写入HTTP输出流，并且置空ls_item以便下行数据    
                    resp.Write(ls_item);
                    ls_item = "";
                }
            }
            else
            {
                if (typeid == "2")
                {
                    //从DataSet中直接导出XML数据并且写到HTTP输出流中
                    resp.Write(ds.GetXml());
                }
            }
            //写缓冲区中的数据到HTTP头文件中
            resp.End();

        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            VipMember bllVip = new VipMember();
            DataSet ds = bllVip.GetList(" where " + strWhere);
            CreateExcel(ds, "1", "Statistics" + ".xls");

        }

        protected void ibtnSearch_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder(" 1=1 ");
            if (this.ddlUnit.SelectedValue != "-1")
            {
                sb.Append(" AND 单位=" + this.ddlUnit.SelectedValue);
            }

            if (this.ddlDepartment.SelectedValue != "-1")
            {
                sb.Append(" AND 部门=" + this.ddlDepartment.SelectedValue);
            }
            if (this.ddlGroup.SelectedValue != "-1")
            {
                sb.Append(" AND 班组=" + this.ddlGroup.SelectedValue);
            }

            if (this.ddlSex.SelectedValue != "-1")
            {
                sb.Append(" AND 性别='" + this.ddlSex.SelectedValue + "'");
            }
            if (this.ddlIsVip.SelectedValue != "-1")
            {
                sb.Append(" AND 会员区分='" + this.ddlIsVip.SelectedValue + "'");
            }
            if (this.ddlAttr.SelectedValue != "-1")
            {
                sb.Append(" AND 属性='" + this.ddlAttr.SelectedValue + "'");
            }
            if (this.txtName.Text.Trim().Length > 0)
            {
                sb.Append(" AND 姓名 like '%" + this.txtName.Text.Trim() + "%'");
            }
            if (this.txtVIPID.Text.Trim().Length > 0)
            {
                sb.Append(" AND 会员证号 like '%" + this.txtVIPID.Text.Trim() + "%'");
            }
            if (this.txtRemark.Text.Trim().Length > 0)
            {
                sb.Append(" AND 备注 like '%" + this.txtRemark.Text.Trim() + "%'");
            }
            if (this.txtRegTimeFrom.Text.Trim().Length > 0)
            {
                sb.Append(" AND 入会时间 >='" + this.txtRegTimeFrom.Text.Trim() + "'");
            }
            if (this.txtRegTimeTo.Text.Trim().Length > 0)
            {
                sb.Append(" AND 入会时间 <= '" + this.txtRegTimeTo.Text.Trim() + "'");
            }
            if (this.ddlTitle.SelectedValue != "-1")
            {
                sb.Append(" AND 职称='" + this.ddlTitle.SelectedValue + "'");
            }
            if (this.ddlPolitical.SelectedValue != "-1")
            {
                sb.Append(" AND 政治面貌='" + this.ddlPolitical.SelectedValue + "'");
            }
            if (this.ddlEducation.SelectedValue != "-1")
            {
                sb.Append(" AND 文化程度='" + this.ddlEducation.SelectedValue + "'");
            }
            strWhere = sb.ToString();
            SetDataInit(1);
        }


        #region 画面事件


        #endregion
    }
